home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 929 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: double const declarations
  5. Date: 01 Apr 1996 10:42:38 PST
  6. Organization: Comp Sci, University of Melbourne
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4jilcf$abp@mulga.cs.mu.OZ.AU>
  9. References: <4jc2fa$bqu@arl-news-svc-2.compuserve.com> <4jdvnj$o35@mulga.cs.mu.OZ.AU> <QQajel16319.199603292150@relay1.UU.NET>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 30 Mar 1996 06:46:39 GMT
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMWAjnky4NqrwXLNJAQHWcQIAs1Lw8xw7apgd9uRkPx+Vo6cjRxygJlPd
  14.     I6vr4Ky7Vs55AdHwIjiZYgerzEONEJF5SKU3wKYskZ30KoPkMIlzeQ==
  15.     =UmF5
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. jchristo@mit.edu (James Christodouleas) writes:
  19.  
  20. >fjh@munta.cs.mu.OZ.AU (Fergus Henderson) wrote:
  21. >
  22. >> Philippe Verdy <100105.3120@compuserve.com> writes:
  23. >> 
  24. >> >template <class T, class CT>
  25. >> >class SmartPtr2  {
  26. >> >   SmartPtr2(T* p) { mp = const_cast<CT>(p) ; }
  27. >> >   CT & Dereference() {
  28. >> >     return *mp ;
  29. >> >   }
  30. >> > private :
  31. >> >   CT *mp ;
  32. >> >} ;
  33. >> >template <class T> class ConstSmartPtr : SmartPtr<T, T> {} ;
  34. >> >template <class T> class FreeSmartPtr : SmartPtr<T, const T> {} ;
  35. >> 
  36. >> You forgot to delegate the constructors.
  37. [...]
  38. >What does it mean to "delegate constructors?"
  39.  
  40. Delegation is when the code to implement an method (in this case,
  41. the constructor) in one class just "delegates" the work to someone
  42. else by calling the same method in another class.
  43.  
  44. Perhaps the simplest way to explain what I meant is to write out the code:
  45.  
  46.     template <class T> class ConstSmartPtr : SmartPtr2<T, T> {
  47.         // delegate the constructor
  48.         CountSmartPtr(T* p) : SmartPtr2(p) {}
  49.     };
  50.     template <class T> class FreeSmartPtr : SmartPtr2<T, const T> {} ;
  51.         // delegate the constructor
  52.         FreeSmartPtr(T* p) : SmartPtr2(p) {}
  53.     };
  54.  
  55. In C++, most operations are automatically delegated from a derived class
  56. to its base class -- this is called inheritence.  However, C++ doesn't
  57. provide any special support for delegating constructors.  That has to be
  58. done manually.
  59.  
  60. I also wrote:
  61.  
  62. >> (Of course, if C++ supported template typedefs, there wouldn't be
  63. >> any need to delegate constructors in examples like this.
  64. >> Unfortunately it doesn't.)
  65.  
  66. In case that wasn't clear, I meant that instead of the above code, if
  67. C++ supported template typedefs, you could write
  68.  
  69.     template <class T> typedef SmartPtr2<T, T> ConstSmartPtr;
  70.     template <class T> typedef SmartPtr2<T, const T> FreeSmartPtr;
  71.  
  72. --
  73. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  74. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  75. ---
  76. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  77.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  78.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  79.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  80.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  81. ]
  82.